package cn.monkey.family.server.domain.uc.auth.verifycode;

import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import java.util.Objects;

public class SimpleCacheableCodeRepository implements CodeRepository {

    private final Cache cache;

    public SimpleCacheableCodeRepository(CacheManager cacheManager) {
        this.cache = Objects.requireNonNull(cacheManager.getCache(CodeRepository.KEY));
    }

    @Override
    public String getCode(String key) {
        String s = this.cache.get(key, String.class);
        if (s != null) {
            this.cache.evict(key);
        }
        return s;
    }

    @Override
    public void setCode(String key, String code) {
        this.cache.put(key, code);
    }
}
